package cn.com.jinhao.provider.user.service.impl;

import cn.com.jinhao.hotline.commons.exception.BusinessErrorException;
import cn.com.jinhao.hotline.commons.vo.business.LinkManInDeptResponseVO;
import cn.com.jinhao.hotline.commons.vo.user.AddressBookRequestVO;
import cn.com.jinhao.provider.user.entity.AddressBook;
import cn.com.jinhao.provider.user.mapper.AddressBookMapper;
import cn.com.jinhao.provider.user.service.AddressBookService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ShaneWei
 */
@Service
public class AddressBookServiceImpl extends CommonService implements AddressBookService {
    @Resource
    private AddressBookMapper addressBookMapper;

    @Override
    public List<LinkManInDeptResponseVO> queryByDeptCode(String deptCode) {
            return addressBookMapper.queryLinkManByDept(deptCode);
    }

    @Override
    public void add(AddressBookRequestVO addressBookRequestVO) throws BusinessErrorException {
        AddressBook addressBook = new AddressBook();
        BeanUtil.copyProperties(addressBookRequestVO, addressBook);
        addressBook.setStat((short)1);
        addressBook.setCode(Integer.toString(this.generateCodeByMax("ST$TLBK")));
        addressBook.setVersion(1);
        int result = addressBookMapper.insertSelective(addressBook);
        if (result == 0) {
            throw new BusinessErrorException("新增联系人信息失败");
        }
    }

    @Override
    public void update(AddressBookRequestVO addressBookRequestVO) throws BusinessErrorException {
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookRequestVO, addressBook);
        int result = addressBookMapper.updateByPrimaryKey(addressBook);
        if (result == 0) {
            throw new BusinessErrorException("更新联系人信息失败");
        }
    }

    @Override
    public void delete(String id) throws BusinessErrorException {
        int result = addressBookMapper.deleteByPrimaryKey(id);
        if (result == 0) {
            throw new BusinessErrorException("删除联系人信息失败");
        }
    }
}
